jz()


function jz(){
    layui.use(['form','laydate','upload'], function(){
        var form = layui.form;
        var upload = layui.upload;
        var laydate = layui.laydate;



        laydate.render({
            elem:'#finish',
            type:'date'
        })

        laydate.render({
            elem:'#wire',
            type:'date'
        })

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#userUrl'
            ,url: '/YltUpload'
            ,accept:'images'
            ,acceptMime:'image/jpg|image/png'
            ,size:50000
            ,before: function(obj){
                obj.preview(function(index, file, result){

                    $('#userUrl').attr('src', result);
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.code > 0){
                    return layer.msg('上传失败');
                }
                //上传成功
                var demoText = $('#demoText');
                demoText.html('<span style="color: #4cae4c;">上传成功</span>');

                var fileupload = $(".image");
                fileupload.attr("value",res.data.fullPath);
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        })

        form.on('submit(*)',function (d) {
            d.field.discountsOriginal = d.field.discountsAmount
            console.log(d.field)

            $.ajax({
                type: 'post',
                url: '/addCoupon',
                data: d.field,
                dataType: 'json',
                success: function (res) {
                    console.log(res)
                    if (res.state == '200'){
                        layer.msg("添加优惠劵成功",{time: 1500,icon:6},function (){
                            let index = parent.layer.getFrameIndex(window.name);
                            //再执行关闭
                            parent.layer.close(index);
                            //刷新主页面
                            //调用自己写的重载方法
                            parent.sxTable();
                        })
                    }else {
                        layer.msg("添加优惠券失败",{time: 1500,icon:5},function (){
                            let index = parent.layer.getFrameIndex(window.name);
                            //再执行关闭
                            parent.layer.close(index);
                        })
                    }

                },
                error: function (e) {
                    console.log("异常");
                    console.log(e);
                }
            })

            return false;
        })

        //点击取消关闭
        form.on('submit(qx)',function (d){
            //获取父级弹窗
            let index = parent.layer.getFrameIndex(window.name);
            //再执行关闭
            parent.layer.close(index);
        })
    })
}
